﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using edowork.com.SQL2005;

namespace WebInvoicingsys.system.settle
{
    public partial class settleclose_list : System.Web.UI.Page
    {
        //前台输出提示信息变量
        public string alert_msg = "";

        //传入的参数
        string years, months;

        /// <summary>
        /// 取得传入的参数
        /// </summary>
        /// <returns></returns>
        private bool GetParma()
        {
            try
            {
                years = Request.Params["years"].ToString();
                months = Request.Params["months"].ToString();
            }
            catch
            {
                //显示出错信息
                alert_msg = String.Format(Scripts.alert_warning, Errormsg.er_Parampost);
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);
                return false;
            }

            return true;
        }


        /// <summary>
        /// 查询数据
        /// </summary>
        /// <returns></returns>
        private DataSet GetData()
        {
            //取得传入的参数
            if (GetParma() == false)
            {
                return null;
            }

            string error_msg = "";

            //构造SQL语句
            string sql = @"select a.sysmid, a.years, a.months, a.sysdate, a.remark, 
                                  (select sum(b.qty2) from sys_monthly_stock b where a.sysmid=b.sysmid) PNqty,
                                  (select sum(c.qty1) from sys_monthly_stock c where a.sysmid=c.sysmid) FNQty,
                                  (select sum(d.qty1) from sys_monthly_bills d where a.sysmid=d.sysmid) Bills
                                from sys_monthly_main a where 1=1";
                             

            sql += (years == "") ? "" : String.Format(" and a.years = '{0}'", years);
            sql += (months == "") ? "" : String.Format(" and a.month = '{0}'", months);

            Execute exec = new Execute(Session["connectionString"].ToString());

            DataSet ds = exec.ExecSQLQueryDataSet(sql, ref error_msg);

            if (ds == null)
            {
                //显示出错信息
                alert_msg = String.Format(Scripts.alert_warning, error_msg);
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);
                return null;
            }

            DataSet dsResult = new DataSet();

            DataTable dt = new DataTable("List");

            dt.Columns.Add("dgvID");
            dt.Columns.Add("sysmid");
            dt.Columns.Add("years");
            dt.Columns.Add("months");
            dt.Columns.Add("sysdate");
            dt.Columns.Add("remark");
            dt.Columns.Add("PNQty");
            dt.Columns.Add("FNQty");
            dt.Columns.Add("Bills");


            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                DataRow drnew = dt.NewRow();

                drnew["dgvID"] = (dt.Rows.Count + 1).ToString();
                drnew["sysmid"] = dr["sysmid"].ToString();
                drnew["years"] = dr["years"].ToString();
                drnew["months"] = dr["months"].ToString();
                drnew["sysdate"] = dr["sysdate"].ToString();
                drnew["remark"] = dr["remark"].ToString();
                drnew["PNQty"] = dr["PNQty"].ToString();
                drnew["FNQty"] = dr["FNQty"].ToString();
                drnew["Bills"] = dr["Bills"].ToString();

                dt.Rows.Add(drnew);
            }

            dsResult.Tables.Add(dt);

            return dsResult;
        }

        /// <summary>
        /// 绑定数据
        /// </summary>
        private void bind()
        {
            DataSet ds = GetData();

            if (ds == null)
            {
                return;
            }

            rplist.DataSource = ds.Tables[0];
            rplist.DataBind();
        }

        /// <summary>
        /// 详细
        /// </summary>
        /// <param name="id"></param>
        private void Detail(string id)
        {
            if (String.IsNullOrEmpty(id))
            {
                return;
            }

            GetParma();

            string url = String.Format("/system/settle/settleclose_detail.aspx?id={0}&years={1}&months={2}", id, years, months);

            Response.Redirect(url);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //查询
                bind();
            }
        }

        //返回
        protected void btnReturn_Click(object sender, EventArgs e)
        {
            string url = "/system/settle/settleclose.aspx";

            Response.Redirect(url);
        }

        //导出
        protected void btnExport_Click(object sender, EventArgs e)
        {
            //取得数据
            DataSet ds = GetData();

            if (ds == null)
            {
                return;
            }

            //构造导出表头
            String[] headname = { "序号", "月结编号", "所在年份", "所在月份", "月结时间", "备注", "期初库存量", "期末库存量", "单据数量" };

            //导出
            ExportToFile.ExportToExcel(ds.Tables[0], HttpContext.Current.Response, headname);
        }

        //详细
        protected void rplist_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            string id = e.CommandArgument.ToString();

            switch (e.CommandName)
            {
                case "Detail":
                    {
                        Detail(id);

                        break;
                    }
                default:
                    {
                        break;
                    }
            }
        }
    }
}